package lily.phs.entertain;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import lily.phs.urh.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class VideoListActivity extends Activity{
	
	TextView tv_TitleBar;
//	private GridView toolbarGrid;
	private ListView listview=null;
	
	private static final String VIDEO_FILE_PATH = "/sdcard/video/";
	private ArrayList<String> videoList;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		this.setContentView(R.layout.media_main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
		
		tv_TitleBar = (TextView)this.findViewById(R.id.title);
        tv_TitleBar.setText("视频列表");
		Button btBack = (Button)this.findViewById(R.id.title_left_btn);
		btBack.setText("后退");
		btBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
			}
		});
		Button btSave = (Button)this.findViewById(R.id.title_right_btn);
		btSave.setVisibility(View.GONE);
		Button btSubmit = (Button)this.findViewById(R.id.title_right_btn2);
		btSubmit.setVisibility(View.GONE);
		
		videoList=getVideoThumbnail();
		listview = (ListView)findViewById(R.id.media_main_List);
		listview.setAdapter(new YaofangItemAdapter(VideoListActivity.this));
		listview.setDivider(null);
		listview.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(VideoListActivity.this,VideoViewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("name",videoList.get(arg2));
				intent.putExtras(bundle);
				startActivity(intent);
				overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
			}
			
		});
	}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == event.KEYCODE_BACK) {
			Intent intent = new Intent();
			intent.setClass(this, YuleActivity.class);
			startActivity(intent);
			finish();
		}
		return true;
    }
	
	class YaofangItemAdapter extends BaseAdapter{
		Context context;
		TextView tvName;

		public YaofangItemAdapter(Context arg2){
			this.context = arg2;
		}

		@Override
		public int getCount(){
			return videoList.size();
		}

		@Override
		public Object getItem(int paramInt){
			return Integer.valueOf(paramInt);
		}

		@Override
		public long getItemId(int paramInt){
			return paramInt;
		}

		@Override
		public View getView(int paramInt, View paramView, ViewGroup paramViewGroup){
			View localView = ((LayoutInflater)this.context.getSystemService(LAYOUT_INFLATER_SERVICE))
			.inflate(R.layout.yaofang_item_view, null);
			ViewHolder mViewHolder = new ViewHolder();
			mViewHolder.tv = (TextView)localView.findViewById(R.id.yaofangItemName);
			if (paramInt == 0){
				String str1 = videoList.get(0);
				mViewHolder.tv.setText(str1);
				localView.setBackgroundResource(R.drawable.circle_list_top);
			}

			if (paramInt >0&&paramInt<(videoList.size()-1)){
				String str2 = videoList.get(paramInt);
				mViewHolder.tv.setText(str2);
				localView.setBackgroundResource(R.drawable.circle_list_middle);
			}	
			
			if(paramInt == (videoList.size()-1)){
				String str3 =  videoList.get(paramInt);
				mViewHolder.tv.setText(str3);
				localView.setBackgroundResource(R.drawable.circle_list_bottom);	
			}

			return localView;
		}
	}
	
	class ViewHolder
	{
		ImageView iv;
		TextView tv;

		ViewHolder()
		{
		}
	}
	
	private ArrayList<String> getVideoThumbnail(){
		
		ArrayList<String> thumbnaiLlist = new ArrayList<String>();
		File folder = new File(VIDEO_FILE_PATH);
		try{
		File [] files = folder.listFiles();
		boolean is3gp = false,ismp4 = false;
		for(int i=0;i<files.length;i++){
			is3gp = files[i].getName().toLowerCase().endsWith(".3gp");
			ismp4 = files[i].getName().toLowerCase().endsWith(".mp4");
			if(is3gp || ismp4){
				Log.e("Files Name", "Files Name = " + files[i].getName());
				
				//Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(VIDEO_FILE_PATH+files[i].getName(), Video.Thumbnails.MINI_KIND);
				String fileName = files[i].getName();
				
				thumbnaiLlist.add(fileName);
			}
		}
		}
		catch(Exception e){
			
		}
		return thumbnaiLlist;
	}

}
